package com.xindaoapp.happypet.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.bean.AboutAttr;
import com.xindaoapp.happypet.bean.CartResponse;
import com.xindaoapp.happypet.bean.GoodsAttr;
import com.xindaoapp.happypet.bean.GoodsAttr.AttrValue;
import com.xindaoapp.happypet.bean.Product;
import com.xindaoapp.happypet.fragments.WriteOrderFragment;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.ProgressHUD;
import com.xindaoapp.happypet.view.MyGridView;

/**
 * 添加频道 购物车
 * 
 * @author Yomine
 */
public class ProductAddActivity extends UMBaseActivity implements OnClickListener {
    @ViewInject(R.id.tittle_shop_cancel)
    private ImageView tittle_shop_cancel;
    @ViewInject(R.id.tittle_shop_content)
    private TextView tittle_shop_content;

    private Product pro;
    @ViewInject(R.id.iv)
    private ImageView iv;
    @ViewInject(R.id.tv)
    private TextView tv;

    @ViewInject(R.id.all_price)
    private TextView all_price;

    @ViewInject(R.id.delete)
    private TextView delete;
    @ViewInject(R.id.add)
    private TextView add;
    @ViewInject(R.id.count)
    private TextView count;
    @ViewInject(R.id.total)
    private TextView total;
    @ViewInject(R.id.add_cart)
    private Button add_cart;

    private int procount;
    private static int goods_number;

    private float price;

    private float allprice;

    private String productId;

    // private String nowbuy;

    @ViewInject(R.id.good_attr)
    private LinearLayout good_attr;

    private int attrSize;
    private List<GoodsAttr> goods_attr;

    private String product_id;

    private String isKill;

    @ViewInject(R.id.loading)
    private FrameLayout loading;

    private List<AboutAttr> aboutAttr;

    private String goods_attr_id = "";

    private int guiGeCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_productadd);
        ViewUtils.inject(this);

        pro = (Product) getIntent().getSerializableExtra("pro");
        if (pro != null) {
            initPro();
        } else {

        }
        productId = pro.goods_id;
        initView();
        initClick();

        if (pro.goods_attr != null && pro.goods_attr.size() > 0) {
            goods_attr = pro.goods_attr;
            initGoodsAttr(goods_attr);
        }
        if (pro.about_attr != null && pro.about_attr.size() > 0) {
            aboutAttr = pro.about_attr;
        }
        // if (!TextUtils.isEmpty(getIntent().getStringExtra("nowbuy"))) {
        // nowbuy = getIntent().getStringExtra("nowbuy");
        // add_cart.setText("立即购买");
        // }
    }

    private void initPro() {
        goods_number = pro.goods_number;
        if (pro.types.equals("0")) {
            price = pro.shop_price;
        } else if (pro.types.equals("1")) {
            if (!TextUtils.isEmpty(getIntent().getStringExtra("isKill"))) {
                isKill = getIntent().getStringExtra("isKill");
            }
            if (isKill.equals("0")) {
                price = pro.shop_price;
            } else {
                price = pro.price_ladder.get(0).price;
            }
        } else if (pro.types.equals("2")) {
            if (!TextUtils.isEmpty(getIntent().getStringExtra("isKill"))) {
                isKill = getIntent().getStringExtra("isKill");
            }
            if (isKill.equals("0")) {
                price = pro.shop_price;
            } else {
                price = pro.price_ladder.get(0).price;
            }
        }
        if (pro.is_activity.equals("1")) {
            add_cart.setText("立即购买");
        } else if (pro.is_activity.equals("0")) {
            int count = 0;
            if (pro.about_attr != null && pro.about_attr.size() > 0) {
                for (int i = 0; i < pro.about_attr.size(); i++) {
                    count = count + Integer.parseInt(pro.about_attr.get(i).is_activity);
                }
            }
            if (count > 0) {
                add_cart.setText("立即购买");
            }
        } else {
            add_cart.setText("加入购物车");
        }
    }

    private int position1 = -1;
    private int position2 = -1;

    private String s1;
    private String s2;

    private int page1 = -1;

    private boolean b1 = false;
    private boolean b2 = false;
    private final boolean b3 = false;
    private final boolean b4 = false;
    private boolean b22 = false;

    private void initGoodsAttr(final List<GoodsAttr> attr) {
        loading.setVisibility(View.GONE);
        int size = attr.size();
        if (size == 1) {
            inflate(attr, 1);
            guiGeCount = 1;
        }
        if (size == 2) {
            inflate(attr, 1);
            inflate(attr, 2);
            guiGeCount = 2;
        }
    }

    private Adapter1 adapter1;
    private MyGridView gv;

    private Adapter2 adapter2;
    private List<AttrValue> attr_value1;
    private List<AttrValue> attr_value2;

    private int oneSize = 0;
    private int twoSize = 0;

    private void inflate(final List<GoodsAttr> attr, final int page) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View inflate = inflater.inflate(R.layout.add_grid, null);
        gv = (MyGridView) inflate.findViewById(R.id.shop_brand);
        TextView tv = (TextView) inflate.findViewById(R.id.attr);
        tv.setText(attr.get(page - 1).attr_name);
        gv.setNumColumns(3);
        if (page == 1) {
            page1 = 1;
            attr_value1 = attr.get(page - 1).attr_value;
            s1 = attr_value1.get(0).goods_attr_id;
            oneSize = attr_value1.size();
            adapter1 = new Adapter1(attr.get(page - 1).attr_value);
            gv.setAdapter(adapter1);

            b1 = true;
            adapter1.setSelectionPosition(0);
            if (guiGeCount == 1) {
                aboutAttr1 = aboutAttr;
            }
            gv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    two.clear();
                    b1 = true;
                    s1 = attr_value1.get(position).goods_attr_id;
                    adapter1.setSelectionPosition(position);
                    if (s1 != null) {
                        if (one.contains(position)) {
                            return;
                        }
                        getNumber2(s1, position);
                        getGoodsNumberAndPrice(s1, page);
                        if (s2 != null) {
                            getGoodsNumberAndPrice(s1 + "|" + s2, page);
                        }
                    }
                    adapter1.notifyDataSetChanged();
                }

            });
        }
        if (page == 2) {
            page1 = 2;
            attr_value2 = attr.get(page - 1).attr_value;
            s2 = attr_value2.get(0).goods_attr_id;
            twoSize = attr_value2.size();
            adapter2 = new Adapter2(attr.get(page - 1).attr_value);

            gv.setAdapter(adapter2);
            adapter2.setSelectionPosition(0);
            gv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    one.clear();
                    b2 = true;
                    if (position == 0) {
                        b22 = true;
                    }
                    position2 = position;
                    s2 = attr_value2.get(position).goods_attr_id;
                    adapter2.setSelectionPosition(position2);
                    if (s2 != null) {
                        if (two.contains(position)) {
                            return;
                        }
                        getNumber1(s2, position);
                        if (s1 != null && s2 != null) {
                            getGoodsNumberAndPrice(s1 + "|" + s2, page);
                        }
                    }
                    adapter2.notifyDataSetChanged();
                }
            });
        }
        LayoutParams lp2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp2.topMargin = 10;
        inflate.setLayoutParams(lp2);
        good_attr.addView(inflate);
    }

    private List<AboutAttr> aboutAttr2;
    private List<AboutAttr> aboutAttr1;

    private void getNumber1(String s22, int position) {
        aboutAttr1 = new ArrayList<AboutAttr>();
        if (guiGeCount == 1) {
            aboutAttr1 = aboutAttr;
        } else {
            if (aboutAttr != null && aboutAttr.size() > 0)
                for (int i = 0; i < aboutAttr.size(); i++) {
                    if (aboutAttr.get(i).goods_attr.contains(s22)) {
                        aboutAttr1.add(aboutAttr.get(i));
                    }
                }
        }
        Collections.sort(aboutAttr1, new SortByBumber1());
        if (adapter1 != null)
            adapter1.notifyDataSetChanged();
    }

    private class SortByBumber1 implements Comparator<AboutAttr> {
        @Override
        public int compare(AboutAttr lhs, AboutAttr rhs) {
            AboutAttr a1 = lhs;
            AboutAttr a2 = rhs;
            String[] split1 = a1.goods_attr.split("\\|");
            String[] split2 = a2.goods_attr.split("\\|");
            if (Integer.valueOf(split1[0]) > Integer.valueOf(split2[0])) {
                return 1;
            } else {
                return -1;
            }
        }
    }

    private void getNumber2(String s11, int position) {
        aboutAttr2 = new ArrayList<AboutAttr>();
        if (aboutAttr != null && aboutAttr.size() > 0)
            for (int i = 0; i < aboutAttr.size(); i++) {
                if (aboutAttr.get(i).goods_attr.contains(s11)) {
                    aboutAttr2.add(aboutAttr.get(i));
                }
            }
        Collections.sort(aboutAttr2, new SortByBumber());
        if (adapter2 != null)
            adapter2.notifyDataSetChanged();
    }

    private class SortByBumber implements Comparator<AboutAttr> {
        @Override
        public int compare(AboutAttr lhs, AboutAttr rhs) {
            AboutAttr a1 = lhs;
            AboutAttr a2 = rhs;
            String[] split1 = a1.goods_attr.split("\\|");
            String[] split2 = a2.goods_attr.split("\\|");
            if (Integer.valueOf(split1[1]) > Integer.valueOf(split2[1])) {
                return 1;
            } else {
                return -1;
            }
        }
    }

    private void getGoodsNumberAndPrice(String attrid, final int page) {
        if (aboutAttr != null && aboutAttr.size() > 0) {
            for (int i = 0; i < aboutAttr.size(); i++) {
                if (aboutAttr.get(i).goods_attr.equals(attrid)) {
                    if (page1 == 1) {
                        if (b1) {
                            loadHave(aboutAttr.get(i));
                        }
                    } else if (page1 == 2) {
                        if (b1 && b2) {
                            loadHave(aboutAttr.get(i));
                        }
                    }
                }
            }
        }
    }

    private void loadHave(AboutAttr aboutAttr2) {
        price = Float.parseFloat(aboutAttr2.product_price);
        procount = Integer.parseInt(aboutAttr2.product_number);
        product_id = aboutAttr2.goods_attr;
        total.setText("库存:" + procount + "件");
        procount = Integer.parseInt(aboutAttr2.product_number);
        allprice =
                Float.parseFloat(aboutAttr2.product_price)
                        * Integer.parseInt(count.getText().toString());
        all_price.setText("￥" + formatDaZhe(allprice) + "");
        product_id = aboutAttr2.goods_attr;
        total.setText("库存:" + procount + "件");

        goods_attr_id = aboutAttr2.product_id;

        goods_number = procount;

        if (pro.is_activity.equals("1")) {
            add_cart.setText("立即购买");
        } else if (pro.is_activity.equals("0")) {
            if (aboutAttr2.is_activity.equals("1")) {
                add_cart.setText("立即购买");
            }
            if (aboutAttr2.is_activity.equals("0")) {
                add_cart.setText("加入购物车");
            }
        } else {
            add_cart.setText("加入购物车");
        }
        if (Integer.valueOf(count.getText().toString()) >= procount) {
            count.setText(procount + "");
            if (procount == 0) {
                count.setText("1");
            }
        }
        if (adapter1 != null) {
            adapter1.notifyDataSetChanged();
        }
        if (adapter2 != null) {
            adapter2.notifyDataSetChanged();
        }
    }

    private final List<Integer> one = new ArrayList<Integer>();
    private boolean isOne1 = true;

    private class Adapter1 extends BaseAdapter {
        private final List<AttrValue> values;

        public Adapter1(List<AttrValue> values) {
            super();
            this.values = values;
        }

        @Override
        public int getCount() {
            return values.size();
        }

        @Override
        public Object getItem(int position) {
            return values.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public void setSelectionPosition(int position) {
            position1 = position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(getApplicationContext(), R.layout.add_text, null);
                holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (position1 == position) {
                if (position == 0) {
                    if (oneSize > 0 && twoSize > 1) {
                        if (isOne1) {
                            getNumber2(s1, -1);
                            getGoodsNumberAndPrice(s1, 1);
                            isOne1 = false;
                        }
                    } else if (oneSize > 0 && twoSize == 0) {

                    }
                    if (b1) {
                        holder.tv1.setBackgroundColor(getResources().getColor(R.color.add_press));
                        holder.tv1.setTextColor(Color.WHITE);
                    }
                } else {
                    holder.tv1.setBackgroundColor(getResources().getColor(R.color.add_press));
                    holder.tv1.setTextColor(Color.WHITE);
                    if (aboutAttr1 != null && aboutAttr1.size() > 0
                            && Integer.parseInt(aboutAttr1.get(position).product_number) == 0) {
                        one.clear();
                        if (b1) {
                            holder.tv1.setBackgroundColor(Color.WHITE);
                            holder.tv1.setTextColor(getResources().getColor(R.color.no_click));
                        } else {
                            holder.tv1.setBackgroundColor(Color.WHITE);
                            holder.tv1.setTextColor(getResources().getColor(
                                    R.color.pop_choice_cancel));
                        }
                        one.add(position);
                    }
                    isOne1 = false;
                }

            } else {
                holder.tv1.setBackgroundColor(Color.WHITE);
                holder.tv1.setTextColor(getResources().getColor(R.color.pop_choice_cancel));
                if (aboutAttr1 != null && aboutAttr1.size() > 0
                        && Integer.parseInt(aboutAttr1.get(position).product_number) == 0) {
                    if (b2) {
                        holder.tv1.setBackgroundColor(Color.WHITE);
                        holder.tv1.setTextColor(getResources().getColor(R.color.no_click));
                    }
                    one.add(position);
                }
            }

            holder.tv1.setText(values.get(position).attr_value);
            adapter1.notifyDataSetChanged();
            return convertView;
        }
    }

    private final List<Integer> two = new ArrayList<Integer>();
    private boolean isOne2 = true;

    private class Adapter2 extends BaseAdapter {
        private final List<AttrValue> values;

        public Adapter2(List<AttrValue> values) {
            super();
            this.values = values;
        }

        @Override
        public int getCount() {
            return values.size();
        }

        @Override
        public Object getItem(int position) {
            return values.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public void setSelectionPosition(int position) {
            position2 = position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(getApplicationContext(), R.layout.add_text, null);
                holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (position2 == position) {
                if (position == 0) {
                    if (oneSize > 0 && twoSize == 1) {
                        if (isOne2) {
                            getNumber1(s2, -1);
                            holder.tv1.setBackgroundColor(getResources()
                                    .getColor(R.color.add_press));
                            holder.tv1.setTextColor(Color.WHITE);
                            if (s1 != null && s2 != null) {
                                getGoodsNumberAndPrice(s1 + "|" + s2, 2);
                            }
                            isOne2 = false;
                        }
                    } else {
                        if (aboutAttr2 != null && aboutAttr2.size() > 0
                                && Integer.parseInt(aboutAttr2.get(0).product_number) == 0) {
                            holder.tv1.setBackgroundColor(Color.WHITE);
                            holder.tv1.setTextColor(getResources().getColor(R.color.no_click));
                            two.add(0);
                        } else {
                            if (b22) {
                                holder.tv1.setBackgroundColor(getResources().getColor(
                                        R.color.add_press));
                                holder.tv1.setTextColor(Color.WHITE);
                            } else {
                                holder.tv1.setBackgroundColor(Color.WHITE);
                                holder.tv1.setTextColor(getResources().getColor(
                                        R.color.pop_choice_cancel));
                            }
                        }
                    }
                } else {
                    holder.tv1.setBackgroundColor(getResources().getColor(R.color.add_press));
                    holder.tv1.setTextColor(Color.WHITE);
                    if (aboutAttr2 != null && aboutAttr2.size() > 0
                            && Integer.parseInt(aboutAttr2.get(position).product_number) == 0) {
                        two.clear();
                        if (b1) {
                            holder.tv1.setBackgroundColor(Color.WHITE);
                            holder.tv1.setTextColor(getResources().getColor(R.color.no_click));
                        }
                        two.add(position);
                    } else {
                        holder.tv1.setBackgroundColor(getResources().getColor(R.color.add_press));
                        holder.tv1.setTextColor(Color.WHITE);
                    }
                }

            } else {
                holder.tv1.setBackgroundColor(Color.WHITE);
                holder.tv1.setTextColor(getResources().getColor(R.color.pop_choice_cancel));
                if (aboutAttr2 != null && aboutAttr2.size() > 0
                        && Integer.parseInt(aboutAttr2.get(position).product_number) == 0) {
                    two.clear();
                    if (b1) {
                        holder.tv1.setBackgroundColor(Color.WHITE);
                        holder.tv1.setTextColor(getResources().getColor(R.color.no_click));
                    }
                    two.add(position);
                }

            }

            holder.tv1.setText(values.get(position).attr_value);
            adapter2.notifyDataSetChanged();
            return convertView;
        }
    }
    class ViewHolder {
        TextView tv1;
    }

    private void initClick() {
        delete.setOnClickListener(this);
        add.setOnClickListener(this);
        add_cart.setOnClickListener(this);
        tittle_shop_cancel.setOnClickListener(this);
    }

    private void initView() {
        // loading.setVisibility(View.VISIBLE);
        tittle_shop_content.setText("规格");
        total.setText("库存:" + goods_number + "件");
        if (pro.thumb_url.size() > 0) {
            ImageLoader.getInstance().displayImage(pro.thumb_url.get(0).url, iv);
        } else {
            iv.setImageResource(R.drawable.picture_loading);
        }
        tv.setText(pro.goods_name);
        all_price.setText("￥" + formatDaZhe(price) + "");
    }

    private String formatDaZhe(Float a) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(a);
    }

    @Override
    public void onClick(View v) {
        procount = Integer.valueOf(count.getText().toString().trim());
        switch (v.getId()) {
            case R.id.add:
                if (procount >= goods_number) {
                    procount = goods_number;
                    Toast.makeText(getApplicationContext(), "库存不足", Toast.LENGTH_SHORT).show();
                } else {
                    procount++;
                }
                count.setText(procount + "");
                all_price.setText("￥" + formatDaZhe(price * procount) + "");
                break;
            case R.id.delete:
                procount = Integer.valueOf(count.getText().toString().trim());
                if (procount > 1) {
                    procount--;
                }
                count.setText(procount + "");
                all_price.setText("￥" + formatDaZhe(price * procount) + "");
                break;
            case R.id.add_cart:
                allprice = price * Integer.valueOf(count.getText().toString().trim());
                if (UserState.isLogged(ProductAddActivity.this)) {
                    if (Integer.parseInt(count.getText().toString()) == 0) {
                        Toast.makeText(getApplicationContext(), "商品数量不能为零", Toast.LENGTH_SHORT)
                                .show();
                        return;
                    } else if (goods_number == 0) {
                        Toast.makeText(getApplicationContext(), "库存不足", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    switch (page1) {
                        case -1:
                            addWebCartData(Integer.parseInt(count.getText().toString().trim()));
                            break;
                        case 1:
                            if (b1) {
                                addWebCartData(Integer.parseInt(count.getText().toString().trim()));
                            } else {
                                Toast.makeText(getApplicationContext(), "请选择您要的商品信息",
                                        Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case 2:
                            if (b2 && b1) {
                                addWebCartData(Integer.parseInt(count.getText().toString().trim()));
                            } else {
                                Toast.makeText(getApplicationContext(), "请选择您要的商品信息",
                                        Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case 3:
                            if (b3 && b1 & b2) {
                                addWebCartData(Integer.parseInt(count.getText().toString().trim()));
                            } else {
                                Toast.makeText(getApplicationContext(), "请选择您要的商品信息",
                                        Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case 4:
                            if (b4 & b1 & b2 & b3) {
                                addWebCartData(Integer.parseInt(count.getText().toString().trim()));
                            } else {
                                Toast.makeText(getApplicationContext(), "请选择您要的商品信息",
                                        Toast.LENGTH_SHORT).show();
                            }
                            break;
                        default:
                            break;
                    }
                }
                break;
            case R.id.tittle_shop_cancel:
                finish();
                break;
            default:
                break;
        }
    }

    private void addWebCartData(final int goodsNumber1) {
        if (CommonUtil.checkNetState(getApplicationContext())) {
            RequestParams params = new RequestParams();
            params.put("goodsID", CommonUtil.decode(productId));
            params.put("userID", CommonUtil.decode(UserState.getUserUid()));
            params.put("goodsNumber", CommonUtil.decode(goodsNumber1 + ""));
            if (add_cart.getText().equals("立即购买")) {
                params.put("type", "1");
            }
            if (!TextUtils.isEmpty(goods_attr_id)) {
                params.put("productID", CommonUtil.decode(goods_attr_id));
            } else {
                if (aboutAttr != null && aboutAttr.size() == 1) {
                    goods_attr_id = aboutAttr.get(0).product_id;
                    params.put("productID", CommonUtil.decode(goods_attr_id));
                } else {
                    params.put("productID", CommonUtil.decode("0"));
                }

            }
            params.put("version", "1.0");
            final ProgressHUD progressHUD = ProgressHUD.show(this, "正在加载...", true, true, null);
            AddClient.get(Address.ADD_TO_CART, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {
                    if (null != progressHUD && progressHUD.isShowing()) {
                        progressHUD.dismiss();
                    }
                    int status = -1;
                    CartResponse parse = JSON.parseObject(content, CartResponse.class);
                    try {
                        status = Integer.valueOf(parse.getStatus());
                        if (status == 1) {
                            if (add_cart.getText().equals("加入购物车")) {
                                Toast.makeText(getApplicationContext(), "加入购物车成功",
                                        Toast.LENGTH_SHORT).show();
                                finish();
                                count.setText("1");
                                total.setText("库存" + (goods_number - goodsNumber1) + "件");
                            }
                            if (add_cart.getText().equals("立即购买")) {
                                if (goods_number == 0) {
                                    Toast.makeText(getApplicationContext(), "库存不足",
                                            Toast.LENGTH_SHORT).show();
                                } else {
                                    Intent intent =
                                            new Intent(ProductAddActivity.this, MainActivity.class);
                                    Constants.ALLPRICE = allprice;
                                    Constants.REC_ID = parse.data.rec_id;
                                    Address.tittle = pro.goods_name;
                                    intent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                                            WriteOrderFragment.class.getCanonicalName());
                                    startActivity(intent);
                                }
                            }
                        } else {
                            Toast.makeText(getApplicationContext(), "购物车中商品大于库存",
                                    Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Throwable error, String content) {
                    if (null != progressHUD && progressHUD.isShowing()) {
                        progressHUD.dismiss();
                    }
                    super.onFailure(error, content);
                }
            });
        } else {
            Toast.makeText(getApplicationContext(), R.string.pLease_check_network, 0).show();
            return;
        }
    }
}
